<?php
namespace app\data\controller\article;

use app\data\model\DataArticleCate;
use think\admin\Controller;

/**
 * 图文分类管理
 * Class GoodsCate
 * @package app\store\controller
 */
class ArticleCate extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    protected $table = 'DataArticleCate';

    /**
     * 图文分类管理
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '图文分类列表';
        $query = $this->_query($this->table)->alias("c")->field("c.*,t.template_name,t.file_name,ct.template_name AS content_template_name,ct.file_name AS content_file_name")
                ->join("data_template t", "c.template_id = t.id", "LEFT")
                ->join("data_template ct", "c.content_template_id = ct.id", "LEFT")
                ->like('c.title#title')->equal('c.status#status');
        $query->where(["c.parent_id" => 0])->order('c.sort desc,c.id asc')->page(false);
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _index_page_filter(&$data)
    {
        foreach ($data as $key => &$vo) {
            $data[$key]['childCount'] = $this->app->db->name("DataArticleCate")->where(["parent_id" => $vo['id']])->count();
            $data[$key]['childList'] = $this->app->db->name("DataArticleCate")->alias("c")
                ->field("c.*,t.template_name,t.file_name,ct.template_name AS content_template_name,ct.file_name AS content_file_name")
                ->join("data_template t", "c.template_id = t.id", "LEFT")
                ->join("data_template ct", "c.content_template_id = ct.id", "LEFT")
                ->where(["c.parent_id" => $vo['id']])->order('c.sort desc,c.id asc')->select()->toArray();
            //三级分类
            foreach ($data[$key]['childList'] AS $k => $v){
                $data[$key]['childList'][$k]['childCount'] = $this->app->db->name("DataArticleCate")->where(["parent_id" => $v['id']])->count();
                $data[$key]['childList'][$k]['childList'] = $this->app->db->name("DataArticleCate")->alias("c")
                    ->field("c.*,t.template_name,t.file_name,ct.template_name AS content_template_name,ct.file_name AS content_file_name")
                    ->join("data_template t", "c.template_id = t.id", "LEFT")
                    ->join("data_template ct", "c.content_template_id = ct.id", "LEFT")
                    ->where(["c.parent_id" => $v['id']])->order('c.sort desc,c.id asc')->select()->toArray();
                //四级分类
                foreach ($data[$key]['childList'][$k]['childList'] AS $k4 => $v4){
                    $data[$key]['childList'][$k]['childList'][$k4]['childCount'] = $this->app->db->name("DataArticleCate")->where(["parent_id" => $v4['id']])->count();
                    $data[$key]['childList'][$k]['childList'][$k4]['childList'] = $this->app->db->name("DataArticleCate")->alias("c")
                        ->field("c.*,t.template_name,t.file_name,ct.template_name AS content_template_name,ct.file_name AS content_file_name")
                        ->join("data_template t", "c.template_id = t.id", "LEFT")
                        ->join("data_template ct", "c.content_template_id = ct.id", "LEFT")
                        ->where(["c.parent_id" => $v4['id']])->order('c.sort desc,c.id asc')->select()->toArray();
                }
            }
        }

    }
    
    /**
     * 批量添加图文分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add_batch()
    {
        $this->title = '批量添加图文分类';
    
        if ($this->request->isGet()) {
            $this->set_parent_id = $_GET['set_parent_id']??0;
        
            if(isset($data['is_page'])){
                $templateType = $data['is_page']?2:0;
            }else{
                $templateType = 0;
            }
        
            //如果每页显示数量为0，则使用系统参数里的设置
            if(empty($data['page_num'])){
                $sys = $this->app->db->name('SystemConfig')->where(['type'=>'base'])->column('name,value');
                $db = [];
                foreach ($sys AS $key => $val){
                    $db[$val['name']] = $val['value'];
                }
                $data['page_num'] = $db['page_num'];
            }
        
            $this->template = $this->app->db->name('DataTemplate')->where(["template_type" => $templateType, "status" => 1])->order("sort desc,id asc")->select();                 //分类模板列表
            $this->content_template = $this->app->db->name('DataTemplate')->where(["template_type" => 1, "status" => 1])->order("sort desc,id asc")->select();        //内容模板列表
        
            $parent_cat = $this->app->db->name('DataArticleCate')->where(["parent_id" => 0])->select()->toArray();
            foreach ($parent_cat as $key => &$vo) {
                $parent_cat[$key]['childCount'] = $this->app->db->name("DataArticleCate")->where(["parent_id" => $vo['id']])->count();
                $parent_cat[$key]['childList'] = $this->app->db->name("DataArticleCate")->alias("c")
                    ->field("c.*,t.template_name,t.file_name,ct.template_name AS content_template_name,ct.file_name AS content_file_name")
                    ->join("data_template t", "c.template_id = t.id", "LEFT")
                    ->join("data_template ct", "c.content_template_id = ct.id", "LEFT")
                    ->where(["c.parent_id" => $vo['id']])->order('c.sort desc,c.id desc')->select()->toArray();
                //三级分类
                foreach ($parent_cat[$key]['childList'] AS $k => $v){
                    $parent_cat[$key]['childList'][$k]['childCount'] = $this->app->db->name("DataArticleCate")->where(["parent_id" => $v['id']])->count();
                    $parent_cat[$key]['childList'][$k]['childList'] = $this->app->db->name("DataArticleCate")->alias("c")
                        ->field("c.*,t.template_name,t.file_name,ct.template_name AS content_template_name,ct.file_name AS content_file_name")
                        ->join("data_template t", "c.template_id = t.id", "LEFT")
                        ->join("data_template ct", "c.content_template_id = ct.id", "LEFT")
                        ->where(["c.parent_id" => $v['id']])->order('c.sort desc,c.id desc')->select()->toArray();
                    //四级分类
                    foreach ($parent_cat[$key]['childList'][$k]['childList'] AS $k4 => $v4){
                        $parent_cat[$key]['childList'][$k]['childList'][$k4]['childCount'] = $this->app->db->name("DataArticleCate")->where(["parent_id" => $v4['id']])->count();
                        $parent_cat[$key]['childList'][$k]['childList'][$k4]['childList'] = $this->app->db->name("DataArticleCate")->alias("c")
                            ->field("c.*,t.template_name,t.file_name,ct.template_name AS content_template_name,ct.file_name AS content_file_name")
                            ->join("data_template t", "c.template_id = t.id", "LEFT")
                            ->join("data_template ct", "c.content_template_id = ct.id", "LEFT")
                            ->where(["c.parent_id" => $v4['id']])->order('c.sort desc,c.id desc')->select()->toArray();
                    }
                }
            }
//            dump($parent_cat);die();
            $this->parent_cat = $parent_cat;
            $this->fetch('form_batch');
        } else {
            $data = $this->request->post();
            
            if($data['is_page'] == 1){
                $data['content_template_id'] = $data['template_id'];    //如果是单页，内容模板ID为单页模板ID

            }
            
            $titleArr = explode("\r\n", $data['title']);
            unset($data['title']);
            //获取分类里内容每页数量的设置
            $sys = $this->app->db->name('SystemConfig')->where(['type'=>'base'])->column('name,value');
            $db = [];
            foreach ($sys AS $key => $val){
                $db[$val['name']] = $val['value'];
            }
            $data['page_num'] = $db['page_num'];
            
            foreach ($titleArr AS $key => $value){
                $data['title'] = $value;
                $this->app->db->name("DataArticleCate")->insert($data);
            }
            $this->success("栏目批量添加成功");
        }
        
        
    }

    /**
     * 添加图文分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        $this->title = '添加图文分类';

        $this->_form($this->table, 'form');
    }

    /**
     * 编辑图文分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->title = '修改图文分类';

        $this->_form($this->table, 'form');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        if ($this->request->isGet()) {
            $this->set_parent_id = $_GET['set_parent_id']??0;

            if(isset($data['is_page'])){
                $templateType = $data['is_page']?2:0;
            }else{
                $templateType = 0;
            }

            //如果每页显示数量为0，则使用系统参数里的设置
            if(empty($data['page_num'])){
                $sys = $this->app->db->name('SystemConfig')->where(['type'=>'base'])->column('name,value');
                $db = [];
                foreach ($sys AS $key => $val){
                    $db[$val['name']] = $val['value'];
                }
                $data['page_num'] = $db['page_num'];
            }

            $this->template = $this->app->db->name('DataTemplate')->where(["template_type" => $templateType, "status" => 1])->order("sort desc,id asc")->select();                 //分类模板列表
            $this->content_template = $this->app->db->name('DataTemplate')->where(["template_type" => 1, "status" => 1])->order("sort desc,id asc")->select();        //内容模板列表

            $parent_cat = $this->app->db->name('DataArticleCate')->where(["parent_id" => 0])->select()->toArray();
            foreach ($parent_cat as $key => &$vo) {
                $parent_cat[$key]['childCount'] = $this->app->db->name("DataArticleCate")->where(["parent_id" => $vo['id']])->count();
                $parent_cat[$key]['childList'] = $this->app->db->name("DataArticleCate")->alias("c")
                    ->field("c.*,t.template_name,t.file_name,ct.template_name AS content_template_name,ct.file_name AS content_file_name")
                    ->join("data_template t", "c.template_id = t.id", "LEFT")
                    ->join("data_template ct", "c.content_template_id = ct.id", "LEFT")
                    ->where(["c.parent_id" => $vo['id']])->order('c.sort desc,c.id desc')->select()->toArray();
                //三级分类
                foreach ($parent_cat[$key]['childList'] AS $k => $v){
                    $parent_cat[$key]['childList'][$k]['childCount'] = $this->app->db->name("DataArticleCate")->where(["parent_id" => $v['id']])->count();
                    $parent_cat[$key]['childList'][$k]['childList'] = $this->app->db->name("DataArticleCate")->alias("c")
                        ->field("c.*,t.template_name,t.file_name,ct.template_name AS content_template_name,ct.file_name AS content_file_name")
                        ->join("data_template t", "c.template_id = t.id", "LEFT")
                        ->join("data_template ct", "c.content_template_id = ct.id", "LEFT")
                        ->where(["c.parent_id" => $v['id']])->order('c.sort desc,c.id desc')->select()->toArray();
                    //四级分类
                    foreach ($parent_cat[$key]['childList'][$k]['childList'] AS $k4 => $v4){
                        $parent_cat[$key]['childList'][$k]['childList'][$k4]['childCount'] = $this->app->db->name("DataArticleCate")->where(["parent_id" => $v4['id']])->count();
                        $parent_cat[$key]['childList'][$k]['childList'][$k4]['childList'] = $this->app->db->name("DataArticleCate")->alias("c")
                            ->field("c.*,t.template_name,t.file_name,ct.template_name AS content_template_name,ct.file_name AS content_file_name")
                            ->join("data_template t", "c.template_id = t.id", "LEFT")
                            ->join("data_template ct", "c.content_template_id = ct.id", "LEFT")
                            ->where(["c.parent_id" => $v4['id']])->order('c.sort desc,c.id desc')->select()->toArray();
                    }
                }
            }
//            dump($parent_cat);die();
            $this->parent_cat = $parent_cat;

        } else {
            if($data['is_page'] == 1){
                $data['content_template_id'] = $data['template_id'];    //如果是单页，内容模板ID为单页模板ID
//                if(!isset($data['id'])){
//                    //因为栏目设置为单页只能有一个单面内容，如果创建栏目时设置了单页类型没有传入栏目ID，则创建单页内容.
//                    //先获得创建单页内容所属的数据表
//                    $tableName = $this->app->db->name('DataTemplate')->where(["id" => $data['content_template_id']])->value('table_name');
//                    $this->app->db->table($tableName)->insert();
//                    dump($data);die();
//                }
            }
        }
    }

    /**
     * 表单结果处理
     * @param boolean $state
     */
    protected function _form_result(bool $state)
    {
        if ($state) {

            $this->success('图文分类设置成功！');
        }
    }

    /**
     * 禁用图文分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        $this->_save($this->table, ['status' => '0']);
    }

    /**
     * 启用图文分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        $this->_save($this->table, ['status' => '1']);
    }

    /**
     * 是否导航显示
     * @auth true
     */
    public function is_nav()
    {
        DataArticleCate::mSave($this->_vali([
            'is_nav.in:0,1'  => '状态值范围异常！',
            'is_nav.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 是否导航显示
     * @auth true
     */
    public function is_best()
    {
        DataArticleCate::mSave($this->_vali([
            'is_best.in:0,1'  => '状态值范围异常！',
            'is_best.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 删除图文分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        if ($this->request->isPost()) {
            $catId = $_POST['id'];
            //判断是否有下级分类
            $childCatList = $this->getChildCat($catId);
            if ($childCatList['countChild'] > 0) {
                $this->error('请先删除所有下级分类！');
            }

            //判断是否为单页，如果是单页则删除单页内容
            $is_page_catInfo = $this->app->db->name('DataArticleCate')->field("id,is_page,content_template_id")->where(['id'=>$catId])->find();    //分类信息
            if($is_page_catInfo['is_page'] == 1){
                $templateInfo = $this->app->db->name('DataTemplate')->field("table_name")->where(['id'=>$is_page_catInfo['content_template_id']])->find();
                //检查此分类的单页是否存在，如果存在则删除！
                $pageCount = $this->app->db->name($templateInfo['table_name'])->field("id")->where(['cat_id'=>$catId])->count();
                if($pageCount > 0){
                    $this->app->db->name($templateInfo['table_name'])->where(['cat_id'=>$catId])->delete();
                }
            }

            //获得所有内容数据表，判断分类下是否有内容
            $tables = $this->app->db->name("DataTemplate")->where("table_name != '' AND template_type > 0")->column('table_name');
            $count = 0;
            foreach ($tables as $key => $t) {
                $thisCount = $this->app->db->name($t)->where('cat_id', $catId)->count("id");
                $count += $thisCount;
            }
            if (!empty($count)) {
                $this->error('此分类及下级分类有图文内容，无法删除！');
            }
        }

        $this->_delete($this->table);
    }

    /* Ajax获取模板列表 */
    public function get_template()
    {
        $type = $_POST['type'];
        $this->template = $this->app->db->name('DataTemplate')->where(["template_type" => $type, "status" => 1])->order("sort desc,id desc")->select();        //分类模板列表
        echo $this->template;
    }

    /**
     * 获取当前分类下所有图文数量和下级分类数量
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function getChildCat($catId)
    {
        $childCatCount = $this->app->db->name('DataArticleCate')->where(['parent_id'=>$catId])->count();    //下级分类数量
//        $childCatList = $this->app->db->name('DataArticleCate')->field("id,parent_id,title,content_template_id")->where(['parent_id'=>$catId])->select()->toArray();
//        return ['count'=>$childCatCount, 'list'=>$childCatList];
        return ['countChild'=>$childCatCount];
    }

}
